package com.mvp.shq.washer.ui.shop.shopAddress;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.ProductAdapter;
import com.mvp.shq.washer.adapter.ShopAddressAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.shop.AddressData;
import com.mvp.shq.washer.entity.shop.result.AddressResult;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.T;
import com.mvp.shq.washer.view.widget.CustomDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopAddressActivity extends BaseActivity<Presenter, Model> implements ShopAddressConstract.View {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.toolbar_menu)
    TextView toolbarMenu;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private ShopAddressAdapter adapter;
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isLoadMore = false;
    private List<AddressData> addressDatas;
    private boolean hasMoreData = true;
    private int clickPosition = -1;
    private int delPosition = -1;
    private CustomDialog mDialog;
    private int TYPE;
    private boolean isManger = false;
    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_address;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        TYPE = getIntent().getIntExtra("type",0);
        initToolBar();
        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        //RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.VERTICAL, 30, R.color.shop_divison_bg);
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 30, ContextCompat.getColor(context, R.color.shop_divison_bg));
        recyclerview.addItemDecoration(divider);
        addressDatas = new ArrayList<>();
        adapter = new ShopAddressAdapter(context,addressDatas, new ShopAddressAdapter.OnAddressClickListener() {
            @Override
            public void onEdit(int position) {
                JumpUtil.toShopAddAddress(context,DataDictionary.ADDRESS_UPDATE,addressDatas.get(position));
            }

            @Override
            public void onDel(int position) {
                CustomDialog.Builder builder = new CustomDialog.Builder(context);
                if (mDialog == null){
                    mDialog = builder.setMessage("是否确认删除此收货地址?")
                            .setNegativeButton("确认", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    delPosition = position;
                                    presenter.delAddress(addressDatas.get(position).getId());
                                    mDialog.dismiss();
                                }
                            })
                            .setPositiveButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mDialog.dismiss();
                                }
                            }).createTwoButtonDialog();
                }
                mDialog.show();

            }

            @Override
            public void onSetDef(int position) {
                clickPosition = position;
                if (addressDatas.get(position).getStatus().equals("1")){
                    return;
                }
                showLoadingDialog(true);
                presenter.setDefAddress(addressDatas.get(position).getId());
            }

            @Override
            public void onItemClick(int position) {
                RxBus.get().send(EventCode.SHOP_CHOOSE_ADDRESS,addressDatas.get(position));
                finish();
            }
        });
        adapter.setTYPE(TYPE);
        recyclerview.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                isLoadMore = false;
                hasMoreData = true;
                pageNo = 1;
                presenter.getAllAddress(pageNo,pageSize);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (!hasMoreData){
                    refreshlayout.finishLoadmore();
                    return;
                }
                isLoadMore = true;
                pageNo ++;
                presenter.getAllAddress(pageNo,pageSize);
            }
        });
        refreshLayout.autoRefresh();
    }

    private void initToolBar(){
        if (TYPE == ShopAddressAdapter.TYPE_CHOOSE){
            title.setText("选择地址");
            toolbarMenu.setText("管理");
        }else if (TYPE == ShopAddressAdapter.TYPE_CHECK){
            title.setText("管理收货地址");
            toolbarMenu.setText("新增");
        }
    }

    @Subscribe(code = EventCode.SHOP_REFRESH_ADDRESS_LIST)
    public void refreshData(){
        refreshLayout.autoRefresh();
    }


    @OnClick({R.id.back, R.id.toolbar_menu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                if (isManger){
                    TYPE = ShopAddressAdapter.TYPE_CHOOSE;
                    initToolBar();
                    adapter.setTYPE(TYPE);
                    adapter.notifyDataSetChanged();
                }else {
                    finish();
                }
                break;
            case R.id.toolbar_menu:
                if (TYPE == ShopAddressAdapter.TYPE_CHECK){
                    JumpUtil.toShopAddAddress(context, DataDictionary.ADDRESS_ADD,null);
                }else if (TYPE == ShopAddressAdapter.TYPE_CHOOSE){
                    TYPE = ShopAddressAdapter.TYPE_CHECK;
                    initToolBar();
                    adapter.setTYPE(TYPE);
                    adapter.notifyDataSetChanged();
                    isManger = true;
                }
                break;
        }
    }

    @Override
    public void getAllAddressS(AddressResult result) {
        if (result.getList() != null){
            if (isLoadMore){
                //加载更多
            }else {
                //刷新
                addressDatas.clear();
            }
            //当前数据最后一条的下标
            int currentNum = (pageNo - 1)*pageSize + result.getList().size();
            if (result.getCount() <= currentNum){
                hasMoreData = false;
            }
            addressDatas.addAll(result.getList());
            adapter.notifyDataSetChanged();
        }
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
    }

    @Override
    public void setDefAddressS() {
        for (int i=0;i<addressDatas.size();i++){
            addressDatas.get(i).setStatus("0");
        }
        addressDatas.get(clickPosition).setStatus("1");
        adapter.notifyDataSetChanged();
    }

    @Override
    public void delAddressS() {
        addressDatas.remove(delPosition);
        adapter.notifyItemRemoved(delPosition);
    }
}
